//
//  HTTableViewController.m
//  UI11_HomeWork
//
//  Created by lanou3g on 15/12/7.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "HTTableViewController.h"
#import "HtTableViewCell.h"
#import "Model.h"
@interface HTTableViewController ()

@property(nonatomic,assign)CGFloat myHeight;

@end

@implementation HTTableViewController



//处理数据
- (instancetype)initWithStyle:(UITableViewStyle)style
{
    if ([super initWithStyle:style]) {
       
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cinemalist" ofType:@"plist"]];
        NSMutableArray *array = dic[@"result"][@"data"];
        self.dataArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            Model *model = [[Model alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        
       
        
    }
    return self;
}




NSString *cell_id = @"cell_id";


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //注册cell
    [self.tableView registerClass:[HtTableViewCell class] forCellReuseIdentifier:cell_id];
    
   
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HtTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id forIndexPath:indexPath];
    
    Model *model = self.dataArray[indexPath.section];
    
    cell.model = model;
    
 
    
    //自适应高度赋值
   _myHeight = [self getCustomHeight:cell.addressLabel.text];
   cell.addressLabel.frame = CGRectMake(5, 50, cell.bounds.size.width - 10, _myHeight);
   
    
    

    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    
    return _myHeight + 90;
}


//自适应高度方法
- (CGFloat)getCustomHeight:(NSString *)str
{
    
    
    
    CGRect rec = [str boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 10, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    return rec.size.height;
    
}








/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
